package com.wzh.microservice.dubbo.provider;
import java.io.IOException;
/**
*@author : 汪振海
*@createdDate : Apr 16, 2020  12:56:45 PM
*@description : <br>
*
*/
import java.util.Properties;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;

/**
 * @author nkorange
 */
public class NamingExample {

    public static void main(String[] args) throws NacosException {

        Properties properties = new Properties();
        properties.setProperty("serverAddr", "http://127.0.0.1:8848");
        properties.setProperty("namespace", "namespace");

        NamingService naming = NamingFactory.createNamingService(properties);

        naming.registerInstance("w", "11.11.11.11", 8888, "TEST1");

        naming.registerInstance("w", "2.2.2.2", 9999, "DEFAULT");
        
       // naming.deregisterInstance("serviceName", instance);
        

        System.out.println("first====="+naming.getAllInstances("w"));

       // naming.deregisterInstance("wangzhenhia", "2.2.2.2", 9999, "DEFAULT");

       // System.out.println("second"+naming.getAllInstances("wangzhenhia"));

        naming.subscribe("w", new EventListener() {
            @Override
            public void onEvent(Event event) {
                System.out.println("onEvent"+((NamingEvent)event).getServiceName());
                System.out.println(((NamingEvent)event).getInstances());
            }
        });
        
        try {
			System.out.print("服务注册成功===");
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.print("over");
    }
}
